<?php
	Header("content-type: application/x-javascript");

	$templatePath = $_GET['templatePath'];
	$eventsUrl = $_GET['eventsUrl'];
	$calendarId = $_GET['calendarId'];
	
	if (isset($_GET['eventId'])) {
		// $eventId = $_GET['eventId'];
		$eventsUrl .= "?evento=".$_GET['eventId'];
	}
?>
// <script type="text/javascript">
;(function() {
	$(document).ready(function() {
		/* Adding functions */
		$.fn.disable = function(){
			this.addClass('disabled');
			return this;
		}
		
		$.fn.enable = function(){
			this.removeClass('disabled');
			return this;
		}
		
		$.fn.toggleEnabled = toggleDisabled = function(){
			this.toggleClass('disabled');
			return this;
		}
	
		try	{
			var calendarId = '<?php echo $calendarId; ?>',
				calendarRoot = $('#' + calendarId),
				buttonsContainer = $(document.createElement('div'))
					.attr('id', calendarId + '-controls')
					.addClass('nav')
					.addClass('button-bar'),
				calendarContainer = $(document.createElement('div')).attr('id', calendarId + '-wrapper');
			

			/* CALENDAR TITLE */
			var calendarTitleEl = $(document.createElement('h2'))
				.addClass('title')
				.addClass('text-center');

			calendarRoot.append(calendarTitleEl, buttonsContainer, calendarContainer);
			
			calendar = calendarContainer.calendar({
				tmplPath: "<?php echo $templatePath; ?>",
				eventsUrl: "<?php echo $eventsUrl; ?>",
				day: 'now',
				firstDay: 2
			});
			
			/*** CREATING CONTROL BUTTONS ***/
			var controlBtnConfig = '<button data-calendar-control-button>';
			
			var controlBtnCls = ['btn'],
				changeViewBtnCls = [];;

			var navigationButtonsGroup = $('<div class="btn-group pull-right">'),
				changeViewButtonsGroup = $('<div class="btn-group" data-toggle="buttons-radio">');
			
			// change to day button
			$(controlBtnConfig)
				.text("Dia")
				.attr('data-view', 'day')
				.addClass(changeViewBtnCls + ' ' + controlBtnCls.join(' '))
				.appendTo(changeViewButtonsGroup);
				
			// change to week button
			$(controlBtnConfig)
				.text("Semana")
				.attr('data-view', 'week')
				.addClass(changeViewBtnCls + ' ' + controlBtnCls.join(' '))
				.appendTo(changeViewButtonsGroup);
			
			// change to month button	
			$(controlBtnConfig)
				.text("Mês")
				.attr('data-view', 'month')
				.addClass(changeViewBtnCls + ' ' + controlBtnCls.join(' '))
				.appendTo(changeViewButtonsGroup);

			// change to year button
			$(controlBtnConfig)
				.text("Ano")
				.attr('data-view', 'year')
				.addClass(changeViewBtnCls + ' ' + controlBtnCls.join(' '))
				.appendTo(changeViewButtonsGroup);
			
			// prev button
			$(controlBtnConfig)
				.attr('data-navigate-button', '')
				.attr('data-navigate-where', 'prev')
				.append('<i class="icon-arrow-left">')
				.addClass(controlBtnCls.join(' '))
				.appendTo(navigationButtonsGroup);
			
			// today button
			$(controlBtnConfig)
				.attr('data-navigate-button', '')
				.attr('data-navigate-where', 'today')
				.append('<i class="icon-calendar">')
				.append(" Hoje")
				.addClass(controlBtnCls.join(' '))
				.appendTo(navigationButtonsGroup);
			
			// next button
			$(controlBtnConfig)
				.attr('data-navigate-button', '')
				.attr('data-navigate-where', 'next')
				.append('<i class="icon-arrow-right">')
				.addClass(controlBtnCls.join(' '))
				.appendTo(navigationButtonsGroup);

			buttonsContainer.append(changeViewButtonsGroup, navigationButtonsGroup, $('<div class="clearfix">')/*clear the float*/);
			
			var viewChangeButtons = $('button[data-view]');
			viewChangeButtons.on('click', function () {
				var btn = $(this);
				calendar.view(btn.data('view'));
				btn.button('toggle');
			});
			
			var navigationButtons = $('button[data-navigate-button]');
			navigationButtons.click(function() {
				var btn = $(this);
				calendar.navigate(btn.data('navigate-where'));
			});

			// Set the active button when the view is changed
			$(calendar).on('viewchange', function(evt, activeView) {
				onViewChange(activeView);
			});

			// Set the active button when the view is changed
			$(calendar).on('navigate', function(evt, where, startDate, endDate) {
				onNavigate(where, startDate, endDate);
			});

			function onViewChange(activeView) {
				$('button[data-view=' + activeView + ']').button('toggle');
				updateTitle();
				updateNavigationButtons();
			}

			function onNavigate(where, startDate, endDate){
				updateTitle();
				updateNavigationButtons();
			}

			function updateTitle(){
				calendarTitleEl.text(calendar.getTitle());
			}

			// Verifies if the "Today" button must be toggled on or not.
			function updateNavigationButtons(){
				var todayBtn = $('button[data-navigate-where=today]');
				if (calendar.isToday())
					todayBtn.addClass('active');
				else
					todayBtn.removeClass('active');
			}

			// The function that must be called only at the first time
			function initCalendar() {
				// Update title for the first time
				updateTitle();
				
				updateNavigationButtons();

				// Set the active view button
				$('button[data-view=' + calendar.options.view + ']').button('toggle');
			}
			initCalendar();
		} catch(e){
			console.error(e.message);
			console.error(e.stack);
		}
	});
})();
// </script>